package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.index.service.IChangeMineInfoService;
import com.forlove.app.qselect.index.service.impl.ChangeMineInfoServiceImpl;
import com.forlove.app.qselect.index.view.ChangeMineInfoView;
import com.forlove.app.qselect.login.entity.SchoolDto;
import com.forlove.app.qselect.login.entity.UserEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/11/21.
 */

public class ChangeMineInfoPresenter extends IBasePresenter<IChangeMineInfoService> {
    private IChangeMineInfoService changeMineInfoService;
    private ChangeMineInfoView changeMineInfoView;

    public ChangeMineInfoPresenter(ChangeMineInfoView changeMineInfoView) {
        this.changeMineInfoView = changeMineInfoView;
        if (changeMineInfoService == null) {
            changeMineInfoService = new ChangeMineInfoServiceImpl();
            setService(changeMineInfoService);
        }
    }

    public void upLoadPic(Map param) {
        changeMineInfoService.uploadFile(ServerManager.ACTION_UPLOAD_IMAGE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像:" + result);
                String photoUrl = ParseHelper.getString(result, "data.img_name");
                if (changeMineInfoView != null) {
                    changeMineInfoView.updateSetPhotoView(photoUrl);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像 onFail:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void loadSchoolInfo() {
        changeMineInfoService.loadData(ServerManager.ACTION_GET_SCHOOL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "学校信息:" + result);
                String school = ParseHelper.getString(result, "data");
                if (StringUtils.isNotEmpty(school)) {
                    SchoolDto schoolDto = JSON.parseObject(school, SchoolDto.class);
                    if (schoolDto != null) {
                        DataManager.saveSchoolInfo(school);
                    }
                    changeMineInfoView.updateSchoolInfo(schoolDto.getData());
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }


    public void loadUpdateUserinfoData(final UserEntity userEntity) {
        Map param = new HashMap();
        param.put("realname", userEntity.realname);
        param.put("sex", userEntity.mSex);
        param.put("scholar", userEntity.scholar);
        param.put("issingle", userEntity.issingle);
        param.put("address", userEntity.address + "");
        param.put("birthday", userEntity.birthday + "");
        param.put("school", userEntity.school + "");
        param.put("yuanxi", userEntity.yuanxi + "");
        changeMineInfoService.loadData(ServerManager.ACTION_UPDATE_USER_INFO, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "设置用户信息:" + result);
                userEntity.parseJSONString(ParseHelper.getString(result, "data.userInfo"));
                if (changeMineInfoView != null) {
                    changeMineInfoView.updateUserInfoView(userEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (changeMineInfoView != null) {
                        changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (changeMineInfoView != null) {
                        changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public void loadSetAccountData(Map param) {

        changeMineInfoService.loadData(ServerManager.ACTION_SET_ACCOUNT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "success 设置账号：" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (changeMineInfoView != null && commonEntity.state == 1) {
                    changeMineInfoView.updateSetAccountView(NetworkManager.ResultCode.RESULT_SUCCESS,
                            commonEntity.message);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (changeMineInfoView != null) {
                        changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (changeMineInfoView != null) {
                        changeMineInfoView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

}
